import MySQLdb
from django.shortcuts import render, redirect
from .models import Student
import json
from urllib.request import urlopen, quote
import requests
# Create your views here.
# 学生信息列表处理函数
conn = MySQLdb.connect(host="localhost", user="root", passwd="123456", db="sms", charset='utf8')

def index(request):
    students = Student.objects.all()

    print(students)
    student_no = request.GET.get('student_no', '')
    student_name = request.GET.get('student_name', '')

    sql = "SELECT id,student_no,student_name FROM sims_student WHERE 1=1 "
    if student_no.strip() != '':
        sql = sql + " and student_no = '" + student_no+"'"
    if student_name.strip() != '':
        sql = sql + " and student_name = '" + student_name+"'"

    print(sql)
    return render(request, 'student/index.html', {'students': students})


# 学生信息新增处理函数
def add(request):
    if request.method == 'GET':
        return render(request, 'student/add.html')
    else:
        student_no = request.POST.get('student_no', '')
        student_name = request.POST.get('student_name', '')
        with conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) as cursor:
            cursor.execute("INSERT INTO sims_student (student_no,student_name) "
                           "values (%s,%s)", [student_no, student_name])
            conn.commit()
        return redirect('../')


# 学生信息修改处理函数
def edit(request):
    if request.method == 'GET':
        id = request.GET.get("id")
        with conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) as cursor:
            cursor.execute("SELECT id,student_no,student_name FROM sims_student where id =%s", [id])
            student = cursor.fetchone()
        return render(request, 'student/edit.html', {'student': student})
    else:
        id = request.POST.get("id")
        student_no = request.POST.get('student_no', '')
        student_name = request.POST.get('student_name', '')
        with conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) as cursor:
            cursor.execute("UPDATE sims_student set student_no=%s,student_name=%s where id =%s",
                           [student_no, student_name, id])
            conn.commit()
        return redirect('../')


# 学生信息删除处理函数
def delete(request):
    id = request.GET.get("id")
    with conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) as cursor:
        cursor.execute("DELETE FROM sims_student WHERE id =%s", [id])
        conn.commit()
    return redirect('../')

ak= "换成自己ak"


class MapResult:
    name =None
    lan = None
    lon = None
def map(request):
    query = request.GET.get('query', '')
    region = request.GET.get('region', '')
    list:MapResult = []

    url = 'https://api.map.baidu.com/place/v2/suggestion?query={0}&city_limit=tru&region={1}&output=json&ak={2}'.format(query, region, ak)
    res = requests.get(url=url)
    if res.status_code == 200:
        temp = json.loads(res.text,strict=False)
        result = temp['result']
        for item in result:
            map_result = MapResult()
            map_result.name=item["name"]
            map_result.lan=item["location"]["lat"]
            map_result.lon=item["location"]["lng"]
            list.append(map_result)

    return render(request, 'student/index.html', {'maplist': list})
